<template>
  <FormModal
    v-model="modalVisible"
    :title="title + '班组'"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRule"
      v-bind="layoutConfig.formLayout"
    >
      `%formCode%`
    </el-form>
    <div slot="footer" v-if="!isPreview">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleOk">
        保存
      </el-button>
    </div>
  </FormModal>
</template>

<script>
const defaultFormData = `%formDataCode%`;
export default {
  name: "InfoModal",
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    mode: {
      type: String,
      default: "add",
    },
    currentInfo: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      layoutConfig: `%layoutConfigCode%`,
      submitLoading: false,
      formData: {
        ...defaultFormData,
      },
      formRule: `%formRuleCode%`,
    };
  },
  computed: {
    modalVisible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      },
    },
    isPreview() {
      return this.mode === "preview";
    },
    title() {
      return this.isPreview ? "查看" : this.mode === "edit" ? "编辑" : "新增";
    },
  },
  watch: {
    value(newVal) {
      if (newVal) {
        this.formData = {
        ...defaultFormData 
        };
      } else {
        this.formData = {
          ...defaultFormData
        };
      }
    },
  },
  methods: {
    async handleOk() {
      try {
        // this.submitLoading = true;
        console.log('formData', this.formData);
        await this.$refs.formRef.validate();
        // const isEdit = !!this.formData.id
        // const remoteMethod = isEdit ? editGroup : addGroup
        // const [err] = await remoteMethod({
        //   ...this.formData,
        // })
        // this.submitLoading = false
        // if (!err) {
        //   this.$message.success(`${isEdit ? '编辑' : '添加'}成功`)
        //   this.$emit('refresh')
        //   this.handleClose()
        // }
      } catch (e) {
        this.submitLoading = false;
        console.error("onSubmit error", e);
      }
    },
    handleClose() {
      this.modalVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.readonly-field {
  padding: 8px 0;
  color: #606266;
}

.el-input__inner {
  border-radius: 4px;
}

.el-form-item {
  margin-bottom: 20px;
}

</style>
